<?php
/**
 *
 *
 * @category     Shuxun Cms
 * @date         2015年10月18日
 * @author       Shuxun Team <kaiping.jiang@scshux.com>
 * @file         ControllerBase.php
 */
namespace Scshux\Front\Controllers;
use Phalcon\Di;
use Phalcon\Di\FactoryDefault;
use Scshux\Plugin\Wechat;
use Scshux\Mvc\SModel;
use Scshux\Mvc\SQuery;
use Scshux\Utils\Variable;
use Scshux\Utils\Tree;
class  ControllerBase extends \Scshux\Mvc\ControllerBase
{
	/**
	 * 初始化
	 */
	public function initialize()
	{
		$this->getDI()->get('view')->setVar('config',$this->getCore()->site->getSiteConfig());
		$this->view->setVar('__siteID', $this->getCore()->getSiteID());

		//banner广告
		$this->view->setVar('bannerad',$this->adv_load(11, 5));
		//获取导航条
		$this->daohang();

		//友情链接
		$this->view->setVar('linklist',$this->getLinkList());
		//业务类型
		$this->view->setVar('ywClass',Variable::getYwClass());
		//面积大小
		$this->view->setVar('ywSize',Variable::getYwSize());
		//官方微信
		$this->view->setvar('qrcode',$this->getQrcode());
		//产品所有分类
		$this->view->setVar('categories', $this->getProCate());

	}


	/**
	 * 获取内核驱动
	 * @return \Scshux\Plugin\Core
	 */
	protected  function  getCore()
	{
		return $this->getDI()->get('core');
	}

	/**
	 * @param integer $positionId
	 * @param integer $limit
	 * 读取广告信息
	 */
	protected function adv_load($positionId,$limit)
	{
	    $model = new SModel('ad');
	    return $model->query('position_id='.$positionId,'link,content,name','id','desc',$limit);
	}

	/**
	 * 获取网站配置
	 * @param  cols
	 */
	protected  function getConfig($cols)
	{
		return  $this->getCore()->site->getSiteConfig();
	}

	/**
	 * 新闻列表
	 * @param catId
	 * @param limit
	 */
	protected function getArticleList($catId=9,$limit=6)
	{
		$return = new \stdClass();

		$Query = new SQuery('article');
		$Query->where = "cat_id ='{$catId}'";
		$Query->fields = "id,title";
		$Query->limit = $limit;
		$return = $Query->find();

		if($return)
		{
			foreach($return as $key=>$val)
			{
				$return[$key]->title = $this->cut_str($val->title, 10) ;
			}
		}
		return $return ;
	}


	/**
	 * 截取字段
	 * @param $str
	 * @param $len
	 * @param $suffix
	 * @author yf
	 */
	protected function cut_str($str,$len,$suffix="..."){
		if(function_exists('mb_substr')){
			if(mb_strlen($str,'utf-8') > $len){
				$str= mb_substr($str,0,$len,'utf-8').$suffix;
			}
			return $str;
		}else{
			if(mb_strlen($str,'utf-8') > $len){
				$str= substr($str,0,$len).$suffix;
			}
			return $str;
		}
	}

	/**
	 * 生成导航栏
	 */
	protected function daohang()
	{
		$array = array('article'=>'新闻中心','product'=>'产品展示');
		$categoryarr = array('article'=>'article_category','product'=>'category') ;
		$tablearr = array('article'=>'article','product'=>'goods') ;
		$indexarr = array('article'=>'id','product'=>'goods_id') ;
		$return = '';
		$requestparam = $this->request->get() ;
		if(!$requestparam || !$requestparam['_url'])
		{
			return false ;
		}
		//解析请求url
		$controller = current(explode('/', trim($requestparam['_url'],'/')));
		if(in_array($controller, array_keys($array)))
		{
			$return .= "<a href='/".$controller."'>".$array[$controller]."</a>";
		}
		else
		{
			return $return ;
		}

		if($requestparam['cid'])
		{
			$cid = $requestparam['cid'];
			//获取类别名称
			$catemodel = new SModel($categoryarr[$controller]);
			$cateinfo = $catemodel->getObj("id = '{$cid}'",'id,name') ;

			if($cateinfo)
			{
				$return .= "> <a href='/".$controller."/index?cid=".$cateinfo->id."'>".$cateinfo->name."</a>";
			}
			else
			{
				return $return ;
			}
		}

		if($requestparam['id'])
		{
			$id = $requestparam['id'] ;
			$indexof = $indexarr[$controller] ;
			//获取类别id

			$tablemodel = new SModel($tablearr[$controller]);
			$tableinfo = $tablemodel->getObj("{$indexof} = '{$id}'",'cat_id') ;

			$cid = $tableinfo->cat_id ;
			$catemodel = new SModel($categoryarr[$controller]);

			$cateinfo = $catemodel->getObj("id = '{$cid}'","id,name") ;

			if($cateinfo)
			{
				$return .= "> <a href='/".$controller."/index?cid=".$cateinfo->$indexof."'>".$cateinfo->name."</a>";
			}
			else
			{
				return $return ;
			}
		}

		$this->view->setVar('daohanglan',$return);

	}

	/**
	 * 前端友情连接
	 */
	protected   function  getLinkList($type=1)
	{
		$filter = array();
		$adModel = new SModel('link');
		$where = ' type=1';

		$items = $adModel->query($where,'link_name,link_url','id','desc');
		return  $items;
	}

	/**
	 * 获取官方微信
	 */
	protected  function getQrcode()
	{
		$qrpath = '';
		$linkModel = new SModel('link');
		$linkinfo = $linkModel->getObj('type=2');

		if($linkinfo)
		{
			$qrpath = $linkinfo->link_img ;
		}

		return $qrpath ;
	}
	/**
	 * 获取所有产品分类
	 *
	 */
	public function getProCate()
	{
		$goodsCategoryModel = new SModel('category');

		//读取所有分类
		$goodsCategoryModel->setFetchMode(\PDO::FETCH_ASSOC);
		$goodsCategories = $goodsCategoryModel->query('site_id=1','*','sort desc,id','desc');
		$treeObj = new Tree($goodsCategories);

		//读取当前语言下所有的分类
		$childCategories = $treeObj->getTree('child',0);

		return $childCategories	;

	}

}